package com.chixing.ih.util;

import com.chixing.ih.module.system.pojo.ToEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author 刘岩
 * @version 1.0
 * @Desc Practice makes perfect
 * @date 2020/9/10 14:17
 */
@Service
public class MailUtil {


    /**
     * 普通邮件发送
     */

    //注入spring的邮箱发送对象
    @Autowired
    JavaMailSenderImpl sender;

    @Async//异步方法
    public void sendEmail(ToEmail toEmail) {
        //创建邮件信息对象
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        //谁发的邮件
        simpleMailMessage.setFrom(toEmail.getFrom());
        //谁要接收
        simpleMailMessage.setTo(toEmail.getTos());
        //邮件标题
        simpleMailMessage.setSubject(toEmail.getSubject());
        //邮件内容
        simpleMailMessage.setText(toEmail.getContent());
        try {
            sender.send(simpleMailMessage);
        } catch (MailException e) {
            e.printStackTrace();
        }

    }

    /**
     * html格式发送邮件
     */
     @Async//异步方法
    public void sendHtmlMail(ToEmail toEmail) throws MessagingException {
        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(toEmail.getFrom());
        helper.setTo(toEmail.getTos());
        helper.setSubject(toEmail.getSubject());
        helper.setText(toEmail.getContent(), true);
        try {
            sender.send(mimeMessage);
        } catch (MailException e) {
            e.printStackTrace();

        }


    }
    @Async//异步方法
    public void sendOneEmail(ToEmail toEmail) {
        //创建邮件信息对象
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        //谁发的邮件
        simpleMailMessage.setFrom(toEmail.getFrom());
        //谁要接收
        simpleMailMessage.setTo(toEmail.getTo());
        //邮件标题
        simpleMailMessage.setSubject(toEmail.getSubject());
        //邮件内容
        simpleMailMessage.setText(toEmail.getContent());
        try {
            sender.send(simpleMailMessage);
        } catch (MailException e) {
            System.out.println("邮件发送失败");
            e.printStackTrace();
        }

    }


    /**
     * 带附件发送，可多个附件 图片，doc文件
     */
     @Async//异步方法
    public void sendEnclosureMail(String[] fileArray, ToEmail toEmail) {
        MimeMessage mimeMessage = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            //发送邮箱的人
            helper.setFrom(toEmail.getFrom());
            //可多人发送，接受邮件的人
            helper.setTo(toEmail.getTos());
            //邮件的标题
            helper.setSubject(toEmail.getSubject());
            //发送邮件的内容
            helper.setText(toEmail.getContent());
            if (null != fileArray) {
                FileSystemResource file = null;
                for (int i = 0; i < fileArray.length; i++) {
                    file = new FileSystemResource(fileArray[i]);
                    helper.addAttachment(fileArray[i].substring(fileArray[i].lastIndexOf(File.separator)), file);
                }
            }
            sender.send(mimeMessage);

        } catch (MessagingException e) {
            e.printStackTrace();

}
}



}
